package com.syni.mdd.yhd.common.all.controller.api.user;

import com.alibaba.fastjson.JSON;
import com.syni.mdd.sjb.common.component.utils.common.utils.ParamsMap;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.sjb.common.component.utils.common.utils.RpcResponseVo;
import com.syni.mdd.sjb.common.component.utils.common.annotations.CheckParams;
import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.constants.ResultCode;
import com.syni.mdd.yhd.common.all.constants.ResultMsg;
import com.syni.mdd.yhd.common.all.controller.api.user.vo.UserTaiFengVo;
import com.syni.mdd.yhd.common.all.entity.user.BmsUser;
import com.syni.mdd.yhd.common.all.service.BmsUserService;
import com.syni.mdd.yhd.common.all.utils.*;
import com.syni.mdd.sjb.common.component.utils.redis.utils.RedisTemplateUtils;;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @className ApiUserController
 * @description TOO
 * @Author cfx
 * @DATE 2019/7/12 18:36
 * @VERSION 1.0
 **/
@RestController
@RequestMapping("/apiUser")
public class ApiUserController {

    @Autowired
    BmsUserService bmsUserService;

    @PostMapping("/updateFreeze")
    @CheckParams(params = {"userId","status"})
    public Map<String,Object> updateFreeze(@RequestBody Map<String,Object> paramsMap){
        Integer userId = Integer.parseInt(paramsMap.get("userId").toString());
        Integer status = Integer.parseInt(paramsMap.get("status").toString());
        return bmsUserService.updateFreezeStatus(userId,status);
    }

    @PostMapping("/registerUser")
    @CheckParams(params = {"phone"})
    public Map<String,Object> registerGenUser(@RequestBody ParamsMap paramsMap){
        String phone = paramsMap.getString("phone");
        String unionId = paramsMap.getString("unionId");
        return bmsUserService.registerUserByExternal(phone,unionId);
    }

    @GetMapping("getUserByUnionId")
    public RpcResponseVo getUserByUnionId(@RequestParam("unionId") String unionId){
        BmsUser bmsUser = bmsUserService.findUserByUnionId(unionId);
        if(bmsUser == null){
            return RpcResponseVo.result(ResultCode.CODE_ERROR_05, ResultMsg.CODE_USER_NO_EXIST);
        }
        UserTaiFengVo userTaiFengVo = new UserTaiFengVo();
        userTaiFengVo.setPhone(bmsUser.getPhone());
        userTaiFengVo.setHeadImg(BmsUserService.getWechatHeadImg(bmsUser));
        userTaiFengVo.setLoginName(BmsUserService.getLoginName(bmsUser));
        return RpcResponseVo.success(userTaiFengVo);
    }

    @GetMapping("getUserByPhone")
    public RpcResponseVo getUserByPhone(@RequestParam("phone") String phone){
        BmsUser bmsUser = bmsUserService.findByPhone(phone);
        if(bmsUser == null){
            return RpcResponseVo.result(ResultCode.CODE_ERROR_05, ResultMsg.CODE_USER_NO_EXIST);
        }
        UserTaiFengVo userTaiFengVo = new UserTaiFengVo();
        userTaiFengVo.setPhone(bmsUser.getPhone());
        userTaiFengVo.setHeadImg(BmsUserService.getWechatHeadImg(bmsUser));
        userTaiFengVo.setLoginName(BmsUserService.getLoginName(bmsUser));
        if(StringUtils.isNotBlank(bmsUser.getUnionid())) {
            userTaiFengVo.setUnionId(bmsUser.getUnionid());
        }
        return RpcResponseVo.success(userTaiFengVo);
    }


    @PostMapping("checkUserIdentity")
    public boolean checkUserIdentity(@RequestBody ParamsMap paramsMap){
        Integer client = paramsMap.getInteger("client");
        Integer userId = paramsMap.getInteger("userIdentify");
        String token = paramsMap.getString("token");
        Map<String,Object> retMap = RequestParamUtils.checkAuthor(userId,token,client);
        if(retMap.containsKey("status") && ResultCode.CODE_NORMAL.equals(retMap.get("status"))) {
            return true;
        }
        return false;
    }

    @PostMapping("/checkZuulIntoUser")
    public RpcResponseVo checkZuulIntoUser(@RequestParam("token") String token){
        if(TokenUtils.checkToken(token,"0") == TokenUtils.TOKEN_SUCCESS){
            Map<String,Object> resultData = new HashMap<>();
            resultData.put("userid",TokenUtils.getUserThreadLocal().get());
            resultData.put("token",token);
            return RpcResponseVo.success(resultData);
        }
        return RpcResponseVo.result(ResultCode.USER_TOKEN_INVALID,ResultMsg.MSG_USER_TOKEN_EXPIRE);
    }

    @PostMapping("/loginByPhoneAndVerCode")
    public Map<String,Object> loginByPhoneAndVerCode(@RequestBody ParamsMap paramsMap){
        String phone = paramsMap.getString("phone");
        String verificationCode = paramsMap.getString("verificationCode");
        Integer createWay = paramsMap.getInteger("createWay");
        String extendData = paramsMap.getString("extendData");
        Map<String,Object> extendDataMap = new HashMap<>();
        if(StringUtils.isNotBlank(extendData)){
            extendDataMap.putAll(JSON.parseObject(extendData));
        }
        return bmsUserService.loginByPhoneAndVerCode(phone,verificationCode,createWay,extendDataMap);
    }

    @PostMapping("/flushUserCache")
    public Map<String,Object> clearUserCache(@RequestBody ParamsMap paramsMap){
        Integer userId = paramsMap.getInteger("userId");
        TokenManager.flushUser(userId);
        return ResultMapHelper.success();
    }

    @GetMapping("/clearUserCache")
    public Map<String,Object> clearUserCache(@RequestParam("userId") Integer userId){
        //获取请求参数头
        String redisKeyTemp = Constants.REDIS_KEY_PREFIX + "userId:" + userId;
        //RequestHeaderData requestHeaderData = new RequestHeaderData();
        if(RedisTemplateUtils.getRedisTemplate().hasKey(redisKeyTemp)){
            RedisTemplateUtils.getRedisTemplate().delete(redisKeyTemp);
        }
        return ResultMapHelper.success();
    }


}
